package nc.bs.gl.eventlistener.bmcontrol;

import nc.bs.businessevent.IEventType;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.gl.hrpbctprule.constants.GlControlRuleConst;
import nc.bs.gl.hrpbctprule.util.GlControlRuleUtil;
import nc.bs.gl.pubinterface.IVoucherDelete;
import nc.bs.gl.voucher.VoucherBO;
import nc.itf.hrpbm.budgetctrl.service.IBmBudgetControlService;
import nc.pubitf.initgroup.InitGroupQuery;
import nc.ui.gl.eventprocess.VoucherEventDispatcher;
import nc.vo.arap.bx.util.BXUtil;
import nc.vo.gl.pubinterface.VoucherOperateInterfaceVO;
import nc.vo.gl.pubvoucher.OperationResultVO;
import nc.vo.gl.pubvoucher.VoucherVO;
import nc.vo.pub.BusinessException;

public class BmVoucherDelete implements IVoucherDelete{

	VoucherVO[] vouchers = null;
	String actionCode = "DELETE";
	@Override
	public OperationResultVO[] afterDelete(
			VoucherOperateInterfaceVO paramVoucherOperateInterfaceVO)
			throws BusinessException {
		// modiff by dyp1
		VoucherEventDispatcher.fireEvent(IEventType.TYPE_DELETE_AFTER, this.vouchers);
		
		boolean isprojControlUsed = (BXUtil.isProductTbbInstalled("M0Z8")
				&& InitGroupQuery.isEnabled(InvocationInfoProxy.getInstance().getGroupId(), "M0Z8"));
		if (!isprojControlUsed)
			return null;
		
		if(vouchers != null && vouchers.length > 0){
			for(int i = 0; i <vouchers.length; i++){
				 
				if(vouchers[i].getPk_system() != null && vouchers[i].getPk_system().equals("PLCF")){
					 continue;
				 }

				//判断业务单据配置节点,是否开启了总账控制规则,如果开启,则走新逻辑,否则走旧的业务逻辑
				if (GlControlRuleUtil.enableGl(GlControlRuleConst.BILLTYPE, null)) {
					//我的新逻辑
					GlControlRuleUtil.processBudgetControl(actionCode,vouchers[i],
							null);
					continue;

				}
				 
				//获取预算控制VO：收入、支出进行分离
				BmCtrlVoucherVOGenerator ctrlvoGenerator = new BmCtrlVoucherVOGenerator();
				AggBmCtrlVoucherVO[] ctrlAggVos = ctrlvoGenerator.generateBmCtrlVO(vouchers[i]);

				/*
				 * 执行预算控制
				 * 先执行收入、再执行支出（支出、资本）
				 */
				String topBudgetCode = null;
				AggBmCtrlVoucherVO ctrlAggVo = null;
				
//				/*
//				 * 执行收入预算
//				 */
//				AggBmCtrlVoucherVO ctrlAggVo_INCOME = ctrlAggVos == null ? null : ctrlAggVos[0];
//				topBudgetCode = "YS01";
//				//再次处理维度信息
//				ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_INCOME);
//				
//				this.budgetCtrl(topBudgetCode, "收入", actionCode, ctrlAggVo, null);	
				
				/*
				 * 执行支出预算
				 */
				AggBmCtrlVoucherVO ctrlAggVo_OUT = ctrlAggVos == null ? null : ctrlAggVos[1];
				topBudgetCode = "YS02";
				//再次处理维度信息
				ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);
				
				this.budgetCtrl(topBudgetCode, "支出", actionCode, ctrlAggVo, null);
				
				
				/*
				 * 执行资本预算
				 */
				topBudgetCode = "YS03";
				//再次处理维度信息
				ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);
				
				this.budgetCtrl(topBudgetCode, "支出", actionCode, ctrlAggVo, null);
				
				/*
				 * 执行资金预算
				 */
				topBudgetCode = "YS05";
				//再次处理维度信息
				ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);
				
				this.budgetCtrl(topBudgetCode, "支出", actionCode, ctrlAggVo, null);
				
			}
		
		}
		
		
		return null;
	}

	/**
	 * 预算控制
	 * @param topBudgetCode
	 * @param otherFilter
	 * @param actionCode
	 * @param ctrlAggVo
	 * @param oldCtrlAggVo
	 * @throws BusinessException
	 */
	private void budgetCtrl(String topBudgetCode,String otherFilter,String actionCode,AggBmCtrlVoucherVO ctrlAggVo, AggBmCtrlVoucherVO oldCtrlAggVo) throws BusinessException{
		
		if(ctrlAggVo != null || oldCtrlAggVo != null){
			NCLocator.getInstance().lookup(IBmBudgetControlService.class).budgetControl(ctrlAggVo.getParentVO().getPk_group(),
					ctrlAggVo.getParentVO().getPk_org(), "C0", null, otherFilter, actionCode, ctrlAggVo, oldCtrlAggVo, new String[]{topBudgetCode});
		}
		
	}
	
	@Override
	public OperationResultVO[] beforeDelete(
			VoucherOperateInterfaceVO paramVoucherOperateInterfaceVO)
			throws BusinessException {
		
		String[] voucherids = paramVoucherOperateInterfaceVO.pk_vouchers;
		
		VoucherBO bo = new VoucherBO();
		vouchers = bo.queryByPks(voucherids);
		VoucherEventDispatcher.fireEvent(IEventType.TYPE_DELETE_BEFORE, this.vouchers);
		
		return null;
	}

}
